package com.farm.shop.service.impl;


import com.farm.device.feign.DeviceFeign;
import com.farm.device.pojo.Device;
import com.farm.entity.PageResult;
import com.farm.farming.pojo.Customer;
import com.farm.shop.dao.CustomerMapper;
import com.farm.shop.dao.DeviceMapper;
import com.farm.shop.dao.FarmShopMapper;
import com.farm.shop.pojo.Shop;
import com.farm.shop.service.FarmShopService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FarmShopServiceImpl implements FarmShopService {
    @Autowired
    private FarmShopMapper farmShopMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Shop> selectAll() {
       return farmShopMapper.selectAll();
    }

    /**
     * 分页查询
     * @param size
     * @param page
     * @return
     */
    @Override
    public PageResult findPage(Integer size, Integer page) {
        PageHelper.startPage(page,size);
        Page<Device> devicepage = (Page<Device>) deviceMapper.selectAll();
        return new PageResult(devicepage.getTotal(),devicepage.getResult());
    }

    /**
     * 根据id删除订单
     * @param orderId
     * @return
     */
    @Override
    public void deleteById(String orderId) {
        List<Shop> shops = farmShopMapper.selectAll();
        for (Shop shop : shops) {
            Integer orderid = shop.getOrderid();
            if(orderId!=null&&!"orderId".equals(orderid)){
                farmShopMapper.deleteByPrimaryKey(orderId);
            }else {
              throw new RuntimeException("没有这个id");
            }
        }

    }
    /**
     * 模糊查询
     * @param map
     * @param size
     * @param page
     * @return
     */
    @Override
    public Page<Device> findPage(Map<String, Object> map, Integer size, Integer page) {
        PageHelper.startPage(size,page);
        Example example = createExample(map);
        return (Page<Device>) deviceMapper.selectByExample(example);
    }

    /**
     * 添加商品
     * @param deviceId
     * @param num
     * @param custId
     */
    @Override
    public void addShop(String deviceId ,Integer num,String custId,String orderId) {
        //查询相关商品的信息
        Shop shop = farmShopMapper.selectByPrimaryKey(orderId);
        if (shop!=null){
            shop.setNum(shop.getNum()+num);
            shop.setAmount(shop.getPrice()*shop.getNum());
            farmShopMapper.updateByPrimaryKey(shop);
        }else {
            //如果商品不存在订单中
            Device device =  deviceMapper.selectByPrimaryKey(deviceId);
            Customer customer = customerMapper.selectByPrimaryKey(custId);
            Shop shop1 = this.device2shop(device, customer, num);
            farmShopMapper.insert(shop1);
        }
        //farmShopMapper.insert(shop);
    }

    private Shop device2shop(Device device,Customer customer, Integer num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        Timestamp ts = Timestamp.valueOf(format);
        Shop shop=new Shop();
        shop.setDeviceid(device.getId());
        shop.setCustid(customer.getId());
        shop.setPrice(device.getPrice());
        shop.setUpdatetime(ts);
        shop.setAmount(device.getPrice()*num);
        shop.setAddressid(customer.getAddr());
        shop.setPhone(customer.getPhone());
        shop.setName(customer.getName());
        shop.setStatus(0);
        shop.setNum(num);
        return shop;
    }


    /**
     * 构建查询对象
     * @param map
     * @return
     */
    private Example createExample(Map<String, Object> map) {
        Example example=new Example(Device.class);
        Example.Criteria criteria = example.createCriteria();
        if (map!=null){
            // 设备id
            if(map.get("id")!=null && !"".equals(map.get("id"))){
                criteria.andEqualTo("id",map.get("id"));
            }
            // 编码
            if(map.get("code")!=null && !"".equals(map.get("code"))){
                criteria.andEqualTo("code",map.get("code"));
            }
            //设备名称
            if(map.get("name")!=null && !"".equals(map.get("name"))){
                criteria.andLike("name","%"+map.get("name")+"%");
            }
            //设备类型
            if(map.get("type")!=null && !"".equals(map.get("type"))){
                criteria.andLike("type","%"+map.get("type")+"%");
            }
            //价格
            if(map.get("price")!=null && !"".equals(map.get("price"))){
                criteria.andLessThanOrEqualTo("price",map.get("price"));
            }
            //品牌brand
            if(map.get("brand")!=null && !"".equals(map.get("brand"))){
                criteria.andLike("brand","%"+map.get("brand")+"%");
            }
            //型号
            if(map.get("model")!=null && !"".equals(map.get("model"))){
                criteria.andEqualTo("model",map.get("model"));
            }
            //应用场景
            if(map.get("scenarios")!=null && !"".equals(map.get("scenarios"))){
                criteria.andLike("scenarios","%"+map.get("scenarios")+"%");
            }
        }
        return example;
    }


}
